package com.open.party.cloud.view.home.jiCengDangJian.partyService

import android.content.Intent
import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.VolunteerVo
import com.open.party.cloud.model.ZyzHuoDongEntity
import com.open.party.cloud.view.home.jiCengDangJian.adapter.ZyzHuoDongListAdapter
import com.open.party.cloud.viewModel.ZiYuanZheViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ZyzHuoDongListForMeActivity : AppRecycleViewTitleBaseActivity<ZyzHuoDongEntity>(), OnSuperListener<ZyzHuoDongEntity> {

    private var ziYuanZheVM: ZiYuanZheViewModel? = null
    private var adapter: ZyzHuoDongListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("我的活动")
        ziYuanZheVM = ZiYuanZheViewModel()
        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = ZyzHuoDongListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingRecyclerView.setLoadingMoreEnabled(false)

        refreshData()
        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }


    override fun loadData(pageVo: PageVo<ZyzHuoDongEntity>) {
        val pageReq = PageReq<VolunteerVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize
        val vo = VolunteerVo()
        pageReq.data = vo

        ziYuanZheVM!!.getHuoDongForMeListData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<ZyzHuoDongEntity>>) {

        if ("getHuoDongForMeListData" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<ZyzHuoDongEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 200) {
            // 成功刷新
            refreshData()
        }
    }

    override fun onClick(position: Int, itemData: ZyzHuoDongEntity, flag: String?) {
        XUtils.intent().openActivity(this@ZyzHuoDongListForMeActivity, ZyzHuoDongDetailActivity::class.java)
            .putStringExtra("id", itemData.id)
            .requestCode(100)
            .start()
    }
}